﻿namespace Digitalmes.Domain.Mes;

/// <summary>
/// SN 流转状态
/// </summary>
/// <remarks>
/// 记录 SN 在线体上当前的状态信息，线体外工位（如返工、抽检等）不记录。
/// </remarks>
[SugarTable("prod_trans_sn")]
public class ProdTransSn : EntityBaseId
{
    /// <summary>
    /// SN
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "SN", Length = 64)]
    public string? Sn { get; set; }

    /// <summary>
    /// 产品 Id。
    /// </summary>
    [SugarColumn(ColumnDescription = "产品Id")]
    public long ProductId { get; set; }

    /// <summary>
    /// 产品代码。
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "产品代码", Length = 64)]
    public string? ProductCode { get; set; }

    /// <summary>
    /// 工单号。
    /// </summary>
    /// <remarks>线外站产品，可设置为空。</remarks>
    [NotNull]
    [SugarColumn(ColumnDescription = "工单号", Length = 32)]
    public string? Wo { get; set; }

    /// <summary>
    /// 当前所在的产线 Id。
    /// </summary>
    public long CurLineId { get; set; }

    /// <summary>
    /// 当前所在的产线编号。
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "当前所在的产线编号", Length = 32)]
    public string? CurLineCode { get; set; }

    /// <summary>
    /// 当前所在的工位 Id。
    /// </summary>
    public long CurOpId { get; set; }

    /// <summary>
    /// 当前所在的工位编号。
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "当前所在的工位编号", Length = 32)]
    public string? CurOpCode { get; set; }

    /// <summary>
    /// 当前所在的工序 Id。没有则为 0。
    /// </summary>
    /// <remarks>线外站时设置为 0.</remarks>
    public long CurProcId { get; set; }

    /// <summary>
    /// 当前所在的工序编号。
    /// </summary>
    /// <remarks>线外站产品，可设置为空。</remarks>
    [NotNull]
    [SugarColumn(ColumnDescription = "当前所在的工序编号", Length = 32)]
    public string? CurProcCode { get; set; }

    /// <summary>
    /// 当前所在的工艺路线 Id。没有则为 0。
    /// </summary>
    /// <remarks>线外站时设置为 0。</remarks>
    public long CurRoutingId { get; set; }

    /// <summary>
    /// 当前所在的工艺路线编号。
    /// </summary>
    /// <remarks>线外站产品，可设置为空。</remarks>
    [NotNull]
    [SugarColumn(ColumnDescription = "当前所在的工艺路线编号", Length = 32)]
    public string? CurRoutingCode { get; set; }

    /// <summary>
    /// 产品在当前工位进站时间
    /// </summary>
    [SugarColumn(ColumnDescription = "当前工位进站时间")]
    public DateTime InboundTime { get; set; }

    /// <summary>
    /// 产品在当前工位出站时间
    /// </summary>
    [SugarColumn(ColumnDescription = "当前工位出站时间")]
    public DateTime? OutboundTime { get; set; }

    /// <summary>
    /// 产品上线时间
    /// </summary>
    /// <remarks>根据工艺路线，起始工位进站时间</remarks>
    [SugarColumn(ColumnDescription = "产品上线时间")]
    public DateTime OnlineTime { get; set; }

    /// <summary>
    /// 产品下线时间
    /// </summary>
    /// <remarks>根据工艺路线，末尾工位出站时间，若是 NG 或报废，更加具体情况设置</remarks>
    [SugarColumn(ColumnDescription = "产品下线时间")]
    public DateTime? OfflineTime { get; set; }

    /// <summary>
    /// 产品当前工作阶段（进站/出站）
    /// </summary>
    [SugarColumn(ColumnDescription = "工作阶段（进站/出站）")]
    public SnStageEnum Stage { get; set; }

    /// <summary>
    /// 产品当前状态。
    /// </summary>
    /// <remarks>产品 NG 后经过返修，同时也会修改对应状态。</remarks>
    [SugarColumn(ColumnDescription = "状态")]
    public SnStatusEnum Status { get; set; }

    /// <summary>
    /// 是否已完工下线。
    /// </summary>
    /// <remarks>
    /// 若是已在工艺路线中的尾部工位且 OK 下件，即为完工状态；
    /// 或是人为设置为完工状态，如根据条件，返修后不再上线，直接完工。
    /// </remarks>
    [SugarColumn(ColumnDescription = "是否已完工")]
    public bool IsCompelted { get; set; }

    /// <summary>
    /// 客户SN
    /// </summary>
    [SugarColumn(ColumnDescription = "客户SN", Length = 64)]
    public string? CustomerSn { get; set; }

    /// <summary>
    /// 返工标识
    /// </summary>
    [SugarColumn(ColumnDescription = "返工标识")]
    public ReworkFlagEnum ReworkFlag { get; set; }

    /// <summary>
    /// 最近一次的返工单据，不存在则为空。
    /// </summary>
    [SugarColumn(ColumnDescription = "返工单据", Length = 32)]
    public string? ReworkDoc { get; set; }

    /// <summary>
    /// 已返修次数。记录该产品返修的总次数。
    /// </summary>
    [SugarColumn(ColumnDescription = "返修次数")]
    public int ReworkCount { get; set; }

    /// <summary>
    /// NG 步序号集合。
    /// </summary>
    /// <remarks>若该工序有设置步序，可在 NG 时对应的步序，没有则为 null。</remarks>
    [SugarColumn(ColumnDescription = "NG步序号集合", IsJson = true, Length = 512)]
    public List<int>? NgSteps { get; set; }

    /// <summary>
    /// 是否需要进行返工，包括 在线返工和离线返工，等待返工或是已返工等待上线，或是正处于返工中。
    /// </summary>
    /// <returns></returns>
    public bool IsNeedRework() => ReworkFlag is ReworkFlagEnum.Wait or ReworkFlagEnum.Working or ReworkFlagEnum.DoneAndWaitToRelaunch;
}
